<template>
	<om-formsub-outer :index="index" :deleteBtn="deleteFlag">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {subOnLoad, formValidation, changeEditor, configSelectData} from '../../../static/js/formUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return {
				formdata: {},
				initValue: {
					targetValueCode:"",
					indexUnitName:'',
					logicRelationshipName:'',
					inputPrecision:"",
					yearRatio:"",
					monthTargetName:""
				},
				autoformdata: [],
				cachename:'',
				index:null,
				datas:[],
				order:0,
				indexCacheKey:"obj-m-tinstmonthlytargetindex-index-cache",
				deleteFlag:false
			}
		},
		methods: {
			beforeConfirm(){
				this.formdata.monthTargetName = 
				(this.formdata.logicRelationshipName || '')
					+ (this.formdata.targetValueCode || '') 
					+ (this.formdata.indexUnitName || '')
			},
			initConfig(e){
				let _that = this
				var arr = _that.datas.filter((e,i) => {
					return (i+1) != _that.order
				});
				if(e.prefix){
					changeEditor(_that.autoformdata[0].editors,"monthTargetCode","prefix",e.prefix)
				}
				if(e.suffix){
					changeEditor(_that.autoformdata[0].editors,"monthTargetCode","suffix",e.suffix)
				}
				changeEditor(_that.autoformdata[0].editors,"itemId","link","../../public/pageSearch/pageSearch")
				changeEditor(_that.autoformdata[0].editors, "itemId", "linkParam", {
					valuefield:"indexId",
					namefield:"indexName",
					extrafield:"indexAttrName",
					notefield:"busRange",
					searchfield:"q",
					other:JSON.stringify({
						posId:e.zposId,
						busCode:e.busCode,
						year:e.year,
						monthCode:e.monthCode,
						arr:JSON.stringify(arr)
					}),
					initload:true,
					url:"tinstAnnualTargetIndex/getIndexList",
					cachename:_that.indexCacheKey
				})
			}
		},
		onLoad(e){
			let _that = this
			subOnLoad(_that, e, function() {
				_that.deleteFlag = JSON.parse(e.deleteFlag)
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								_that.initConfig(e)
							}
						}
					});
				}
			})
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key: this.indexCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.busRange = d.busRange
							formdata.itemId = d.indexId
							formdata.itemName = d.indexName
							formdata.targetValueCode = d.targetValueCode
							formdata.targetValueName = d.targetValueName
							formdata.indexUnitName = d.indexUnitName
							formdata.logicRelationshipName = d.logicRelationshipName
							formdata.inputPrecision = d.inputPrecision || d.indexPrecision
							formdata.yearRatio = d.ratioName
							if(d.logicRelationshipName){
								_that.$refs.autoform.changeEditor("indexInfo","monthTargetCode","prefix",d.logicRelationshipName)
								_that.$refs.autoform.changeEditor("indexInfo","monthTargetCode","prefixCol",d.logicRelationshipName.length)
							}
							if(d.indexUnitName){
								_that.$refs.autoform.changeEditor("indexInfo","monthTargetCode","suffix",d.indexUnitName)
								_that.$refs.autoform.changeEditor("indexInfo","monthTargetCode","suffixCol",d.indexUnitName.length)
								if(d.indexUnitName == "%"){
									_that.$refs.autoform.changeEditor("indexInfo","monthTargetCode","min",0)
									_that.$refs.autoform.changeEditor("indexInfo","monthTargetCode","max",100)
								}
							}
							uni.removeStorage({
								key: _that.indexCacheKey
							});
							
						}
					}
				});
				
			}
		}
	}
</script>

<style>

</style>
